
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

function ENT:Initialize()

	self.Entity:SetName("Shockwave")
	self.Entity:SetModel( "models/items/combine_rifle_ammo01.mdl" )
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )

	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableGravity(false)
		phys:EnableDrag(false)
		phys:EnableCollisions(false)
	end

	self.PhysObj = self.Entity:GetPhysicsObject()

	self.Entity:SetColor(0,255,0,0)

	self.Gas = ents.Create("env_smoketrail")
	self.Gas:SetAngles( self.Entity:GetAngles()  )
	self.Gas:SetPos( self.Entity:GetPos() )
	self.Gas:SetParent( self.Entity )
	self.Gas:SetKeyValue("startcolor", "180 140 80")
	self.Gas:SetKeyValue("endcolor", "200 170 129")
	self.Gas:SetKeyValue("startsize","50")
	self.Gas:SetKeyValue("endsize","100")
	self.Gas:SetKeyValue("lifetime", "2")
	self.Gas:SetKeyValue("opacity", ".5")
	self.Gas:SetKeyValue("spawnrate","10")
	self.Gas:Spawn()
	self.Gas:Activate()

	local FlatteningShockwavePain = ents.Create( "point_hurt" )
	FlatteningShockwavePain:SetName("Shockwave")
	FlatteningShockwavePain:SetParent( self.Entity )
	FlatteningShockwavePain:SetKeyValue("DamageRadius", "250" )
	FlatteningShockwavePain:SetKeyValue("Damage", "10" )
	FlatteningShockwavePain:SetKeyValue("DamageDelay", "0.1" )
	FlatteningShockwavePain:SetKeyValue("DamageType", "1" )
	FlatteningShockwavePain:SetPos( self.Entity:GetPos() )
	FlatteningShockwavePain:Fire("turnon", "", 0);
	FlatteningShockwavePain:Spawn()
	FlatteningShockwavePain:Activate()

	self.ShakeIt = ents.Create( "env_shake" )
	self.ShakeIt:SetName("Shaker")
	self.ShakeIt:SetParent( self.Entity )
	self.ShakeIt:SetKeyValue("amplitude", "250" )
	self.ShakeIt:SetKeyValue("radius", "100" )
	self.ShakeIt:SetKeyValue("duration", "5" )
	self.ShakeIt:SetKeyValue("frequency", "255" )
	self.ShakeIt:SetPos( self.Entity:GetPos() )
	self.ShakeIt:Fire("StartShake", "", 0);
	self.ShakeIt:Spawn()
	self.ShakeIt:Activate()
end

function ENT:PhysicsUpdate()

end

function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end

	local SpawnPos = tr.HitPos + tr.HitNormal * 36

	local ent = ents.Create( "swave" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:Think()

	self.Entity:SetColor(0,255,0,0)

	self.ShakeIt:Fire("StartShake", "", 0);	

	local BVic = ents.FindInSphere( self.Entity:GetPos(), 200 )
	
	for x, c in pairs(BVic) do
		local breakcase = c.hasdamagecase or false
		if (c:IsVehicle() || c:GetClass() == "gmod_wheel" || c:GetClass() == "prop_physics" || breakcase == true) then
			local  gdmg = math.random(1,20)
			attack = cbt_dealdevhit(c.Entity, gdmg, 10)
			if (attack != nil) then
				if (attack == 2) then
					c.Entity.cbt.health = 1
				end
				if (attack >= 1) then
					local phys = c.Entity:GetPhysicsObject()
					phys:SetVelocity( self.Entity:GetForward() * 10000 + self.Entity:GetUp() * 10000 )
				end
			end
		end
	end

	--self.Entity:Remove()
end

function ENT:PhysicsCollide( data, physobj )
	
end

function ENT:OnTakeDamage( dmginfo )
	
end

function ENT:Use( activator, caller )

end